package com.enjoyor.soa.traffic.server.efoms.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.enjoyor.soa.traffic.api.dto.efoms.EvalGradeInfoDto;
import com.enjoyor.soa.traffic.api.dto.efoms.FileInfoDto;
import com.enjoyor.soa.traffic.api.dto.efoms.PingConnectRealDto;
import com.enjoyor.soa.traffic.api.dto.efoms.PingResultDto;
import com.enjoyor.soa.traffic.api.dto.efoms.RepairsInfoDto;
import com.enjoyor.soa.traffic.api.dto.efoms.WorkordersInfoDto;
import com.enjoyor.soa.traffic.api.dto.efoms.WorkordersRecordInfoDto;
import com.enjoyor.soa.traffic.api.ubms.dto.DeviceInfoDto;
import com.enjoyor.soa.traffic.core.efoms.domain.DeviceStatusInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.EvalGradeInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.FileInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.PingConnectReal;
import com.enjoyor.soa.traffic.core.efoms.domain.RepairsInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.WorkordersInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.WorkordersRecordInfo;
import com.enjoyor.soa.traffic.core.efoms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.core.efoms.helper.GetObjectHelper;
import com.enjoyor.soa.traffic.util.helper.ReflectHelper;

/*
 * @description:对象之间转换；
 */

public class ObjectSwitchHelper {
	
	private static final Logger logger = Logger.getLogger(ObjectSwitchHelper.class);

	/*
	 * 维修单对象转化为工单对象
	 */
	public static WorkordersInfo repairsToWorkorders(RepairsInfo repairsInfo) {
		WorkordersInfo workordersInfo = new WorkordersInfo();
		workordersInfo.setConPerson(repairsInfo.getContactPerson());
		workordersInfo.setConPersonTel(repairsInfo.getContactPersonTel());
		workordersInfo.setDevAreaCode(repairsInfo.getDevAreaCode());
		workordersInfo.setDevAreaName(repairsInfo.getDevAreaName());
		workordersInfo.setDevBrand(repairsInfo.getDevBrand());
		workordersInfo.setDevBuildDate(repairsInfo.getDevBuildDate());
		workordersInfo.setDevBuildPosition(repairsInfo.getDevBuildPosition());
		workordersInfo.setDevCategoryCode(repairsInfo.getDevCategoryCode());
		workordersInfo.setDevCategoryName(repairsInfo.getDevCategoryName());
		workordersInfo.setDevDeptId(repairsInfo.getDevDeptId());
		workordersInfo.setDevDeptName(repairsInfo.getDevDeptName());
		workordersInfo.setDevFactory(repairsInfo.getDevFactory());
		workordersInfo.setDevIp(repairsInfo.getDevIp());
		workordersInfo.setDevId(repairsInfo.getDevId());
		workordersInfo.setDevModel(repairsInfo.getDevModel());
		workordersInfo.setDevName(repairsInfo.getDevName());
		workordersInfo.setDevTypeCode(repairsInfo.getDevTypeCode());
		workordersInfo.setDevTypeName(repairsInfo.getDevTypeName());
		workordersInfo.setFailureDescrible(repairsInfo.getFailureDescrible());
		workordersInfo.setFailureTypeCode(repairsInfo.getFailureTypeCode());
		workordersInfo.setFailureTypeName(repairsInfo.getFailureTypeName());
		workordersInfo.setLatitude(repairsInfo.getLatitude());
		workordersInfo.setLongitude(repairsInfo.getLatitude());
		workordersInfo.setRepairsId(repairsInfo.getRepairsId());
		workordersInfo.setRepDate(repairsInfo.getRepDate());
		workordersInfo.setRepDeptId(repairsInfo.getRepDeptId());
		workordersInfo.setRepDeptName(repairsInfo.getRepDeptName());
		workordersInfo.setRepPersonName(repairsInfo.getRepPersonName());
		workordersInfo.setRepPersonId(repairsInfo.getRepPersonId());
		workordersInfo.setRepPersonTel(repairsInfo.getRepPersonTel());
		workordersInfo.setRepSourceCode(repairsInfo.getRepSourceCode());
		workordersInfo.setRepSourceName(repairsInfo.getRepSource());
		workordersInfo.setFileInfoList(repairsInfo.getFileList());
		workordersInfo.setIsRpeApply(false);
		return workordersInfo;
	}
	
	/*
	 * 设备信息转化报修单信息；
	 */
	public static RepairsInfo UbmsDeviceToRepairsInfo(DeviceInfoDto deviceInfoDto,RepairsInfo repairsInfo){
		repairsInfo.setDevAreaCode(deviceInfoDto.getAreaCode());
		repairsInfo.setDevAreaName(deviceInfoDto.getAreaName());
		repairsInfo.setDevBrand(deviceInfoDto.getBrand());
		repairsInfo.setDevBuildDate(deviceInfoDto.getBuildDate());
		repairsInfo.setDevBuildPosition(deviceInfoDto.getPosition());
		repairsInfo.setDevDeptId(deviceInfoDto.getDeptId());
		repairsInfo.setDevDeptName(deviceInfoDto.getDeptName());
		repairsInfo.setDevFactory(deviceInfoDto.getFactory());
		repairsInfo.setDevId(deviceInfoDto.getDevId());
		repairsInfo.setDevIp(deviceInfoDto.getIp());
		repairsInfo.setDevModel(deviceInfoDto.getModel());
		Double latitude = Double.parseDouble(deviceInfoDto.getLatitude());
		repairsInfo.setLatitude(latitude);
		Double longitude = Double.parseDouble(deviceInfoDto.getLongitude());
		repairsInfo.setLongitude(longitude);
		return repairsInfo;
	}
	
	/*
	 * 根据报修单信息获得设备状态对象；
	 */
	public static DeviceStatusInfo RepairsToDevStatusInfo(RepairsInfo repairsInfo){
		DeviceStatusInfo deviceStatusInfo = new DeviceStatusInfo();
		deviceStatusInfo.setDevId(repairsInfo.getDevId());
		deviceStatusInfo.setDevStatusCode(EnumDicPublic.DEV_REP_STATUS_REPAIRING.getKey());
		deviceStatusInfo.setDevStatusName(EnumDicPublic.DEV_REP_STATUS_REPAIRING.getValue());
		deviceStatusInfo.setDevTypeCode(repairsInfo.getDevTypeCode());
		deviceStatusInfo.setDevTypeName(repairsInfo.getDevTypeName());
		deviceStatusInfo.setDevName(repairsInfo.getDevName());
		deviceStatusInfo.setRepStatusCode(EnumDicPublic.DEVICE_STATUS_FAILURE.getKey());
		deviceStatusInfo.setRepStatusName(EnumDicPublic.DEVICE_STATUS_FAILURE.getValue());
		return deviceStatusInfo;
	} 
	
	/*
	 * 设备信息赋值到维修工单信息；
	 */
	public static WorkordersInfo UbmsDeviceToWorkordersInfoDto(DeviceInfoDto deviceInfoDto,WorkordersInfo workordersInfo){
		workordersInfo.setDevAreaCode(deviceInfoDto.getAreaCode());
		workordersInfo.setDevAreaName(deviceInfoDto.getAreaName());
		workordersInfo.setDevBrand(deviceInfoDto.getBrand());
		workordersInfo.setDevBuildDate(deviceInfoDto.getBuildDate());
		workordersInfo.setDevBuildPosition(deviceInfoDto.getPosition());
		workordersInfo.setDevDeptId(deviceInfoDto.getDeptId());
		workordersInfo.setDevDeptName(deviceInfoDto.getDeptName());
		workordersInfo.setDevFactory(deviceInfoDto.getFactory());
		workordersInfo.setDevId(deviceInfoDto.getDevId());
		workordersInfo.setDevIp(deviceInfoDto.getIp());
		workordersInfo.setDevModel(deviceInfoDto.getModel());
		Double latitude = Double.parseDouble(deviceInfoDto.getLatitude());
		workordersInfo.setLatitude(latitude);
		Double longitude = Double.parseDouble(deviceInfoDto.getLongitude());
		workordersInfo.setLongitude(longitude);
		return workordersInfo;
	}
	
	/**
	 * 将维修工单信息Pojo类转换成Dto类
	 * 如果内部存在List，一起转换
	 * @param list
	 * @return
	 */
	public static List<WorkordersInfoDto> workordersPojoToDtoList(List<WorkordersInfo> listPojo) {
		List<WorkordersInfoDto> listDto = new ArrayList<WorkordersInfoDto>();
		try {
			if(listPojo == null || listPojo.size() <= 0) {
				return listDto;
			}
			for (WorkordersInfo workordersInfo : listPojo) {
				listDto.add(workordersPojoToDto(workordersInfo));
			}
		}catch(Exception ex){
			logger.info(String.format("workordersPojoToDtoList error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return listDto;
		}
		return listDto;
	}
	
	public static WorkordersInfoDto workordersPojoToDto(WorkordersInfo pojo) {
		WorkordersInfoDto dto = new WorkordersInfoDto();
		try {
			if(pojo == null) {
				return dto;
			}
				
			dto = ReflectHelper.beanToBean(pojo, WorkordersInfoDto.class);
			List<FileInfo> fileInfoList = pojo.getFileInfoList();
			if(null != fileInfoList && fileInfoList.size() > 0) {
				dto.setFileInfoList(ReflectHelper.listToList(fileInfoList, FileInfoDto.class));
			}
			
			List<WorkordersRecordInfo> workordersRecordList = pojo.getWorkordersRecordList();
			if(null != workordersRecordList && workordersRecordList.size() > 0) {
				dto.setWorkordersRecordList(workordersRecordPojoToDtoList(workordersRecordList));
			}
		}catch(Exception ex){
			logger.info(String.format("workordersPojoToDto error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return dto;
		}
		return dto;
	}
	/**
	 * 将维修工单Dto类转换成Pojo类
	 * 如果内部存在List，一起转换
	 * @param list
	 * @return
	 */
	public static List<WorkordersInfo> workordersDtoToPojoList(List<WorkordersInfoDto> listDto) {
		List<WorkordersInfo> listPojo = new ArrayList<WorkordersInfo>();
		try {
			if(listDto == null || listDto.size() <= 0) {
				return listPojo;
			}
			for (WorkordersInfoDto workordersInfoDto : listDto) {
				listPojo.add(workordersDtoToPojo(workordersInfoDto));
			}
		}catch(Exception ex){
			logger.info(String.format("workordersDtoToPojoList error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return listPojo;
		}
		return listPojo;
	}
	public static WorkordersInfo workordersDtoToPojo(WorkordersInfoDto dto){
		WorkordersInfo pojo = new WorkordersInfo();
		try {
			if(dto == null) {
				return pojo;
			}
			pojo = ReflectHelper.beanToBean(dto, WorkordersInfo.class);
			List<FileInfoDto> fileInfoDtoList = dto.getFileInfoList();
			if(null != fileInfoDtoList && fileInfoDtoList.size() > 0) {
				pojo.setFileInfoList(ReflectHelper.listToList(fileInfoDtoList, FileInfo.class));
			}
			List<WorkordersRecordInfoDto> workordersRecordDtoList = dto.getWorkordersRecordList();
			if(null != workordersRecordDtoList && workordersRecordDtoList.size() > 0) {
				pojo.setWorkordersRecordList(workordersRecordDtoToPojoList(workordersRecordDtoList));
			}
			Map<String, List<WorkordersRecordInfo>> workordersRecordMap = new HashMap<>();
			Map<String, List<WorkordersRecordInfoDto>> workordersRecordDtoMap = dto.getWorkordersRecordMap();
			if(null != workordersRecordDtoMap) {
	    		List<WorkordersRecordInfoDto> dispatchList = workordersRecordDtoMap.get("dispatchList");
	    		if(null != dispatchList && dispatchList.size() > 0) {
	    			workordersRecordMap.put("dispatchList", workordersRecordDtoToPojoList(dispatchList));
	    		}
	    		List<WorkordersRecordInfoDto> fackbackList = workordersRecordDtoMap.get("fackbackList");
	    		if(null != fackbackList && fackbackList.size() > 0) {
	    			workordersRecordMap.put("fackbackList", workordersRecordDtoToPojoList(fackbackList));
	    		}
	    		pojo.setWorkordersRecordMap(workordersRecordMap);
			}
		} catch(Exception ex) {
			//LoggerHelper.LOG.error("getEventInfoPojo error: " + ex.getMessage());
			logger.info(String.format("workordersDtoToPojo error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return pojo;
		}
		return pojo;
	}
	/**
	 * 将维修工单操作记录信息Pojo类转换成Dto类
	 * 如果内部存在List，一起转换
	 * @param list
	 * @return
	 */
	public static List<WorkordersRecordInfoDto> workordersRecordPojoToDtoList(List<WorkordersRecordInfo> listPojo) {
		List<WorkordersRecordInfoDto> listDto = new ArrayList<WorkordersRecordInfoDto>();
		try {
			if(listPojo == null || listPojo.size() <= 0) {
				return listDto;
			}
			for (WorkordersRecordInfo workordersRecordInfo : listPojo) {
				listDto.add(workordersRecordPojoToDto(workordersRecordInfo));
			}
		}catch(Exception ex){
			logger.info(String.format("workordersRecordPojoToDtoList error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return listDto;
		}
		return listDto;
	}
	
	public static WorkordersRecordInfoDto workordersRecordPojoToDto(WorkordersRecordInfo pojo) {
		WorkordersRecordInfoDto dto = new WorkordersRecordInfoDto();
		try {
			if(pojo == null) {
				return dto;
			}
			dto = ReflectHelper.beanToBean(pojo, WorkordersRecordInfoDto.class);
			List<FileInfo> fileInfoList = pojo.getFileInfoList();
			if(null != fileInfoList && fileInfoList.size() > 0) {
				dto.setFileInfoList(ReflectHelper.listToList(fileInfoList, FileInfoDto.class));
			}
			List<EvalGradeInfo> evalGradeList = pojo.getEvalGradeList();
			if(null != evalGradeList && evalGradeList.size() > 0) {
				dto.setEvalGradeList(ReflectHelper.listToList(evalGradeList, EvalGradeInfoDto.class));
			}
		}catch(Exception ex){
			logger.info(String.format("workordersRecordPojoToDto error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return dto;
		}
		return dto;
	}
	/**
	 * 将维修工单操作记录Dto类转换成Pojo类
	 * 如果内部存在List，一起转换
	 * @param list
	 * @return
	 */
	public static List<WorkordersRecordInfo> workordersRecordDtoToPojoList(List<WorkordersRecordInfoDto> listDto) {
		List<WorkordersRecordInfo> listPojo = new ArrayList<WorkordersRecordInfo>();
		try {
			if(listDto == null || listDto.size() <= 0) {
				return listPojo;
			}
			for (WorkordersRecordInfoDto workordersRecordInfoDto : listDto) {
				listPojo.add(workordersRecordDtoToPojo(workordersRecordInfoDto));
			}
		}catch(Exception ex){
			logger.info(String.format("workordersRecordDtoToPojoList error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return listPojo;
		}
		return listPojo;
	}
	public static WorkordersRecordInfo workordersRecordDtoToPojo(WorkordersRecordInfoDto dto){
		WorkordersRecordInfo pojo = new WorkordersRecordInfo();
		try {
			if(dto == null) {
				return pojo;
			}
			pojo = ReflectHelper.beanToBean(dto, WorkordersRecordInfo.class);
			List<FileInfoDto> fileInfoDtoList = dto.getFileInfoList();
			if(null != fileInfoDtoList && fileInfoDtoList.size() > 0) {
				pojo.setFileInfoList(ReflectHelper.listToList(fileInfoDtoList, FileInfo.class));
			}
			List<EvalGradeInfoDto> evalGradeDtoList = dto.getEvalGradeList();
			if(null != evalGradeDtoList && evalGradeDtoList.size() > 0) {
				pojo.setEvalGradeList(ReflectHelper.listToList(evalGradeDtoList, EvalGradeInfo.class));
			}
		} catch(Exception ex) {
			logger.info(String.format("workordersRecordDtoToPojo error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return pojo;
		}
		return pojo;
	}
	
	/**
	 * 将ping结果List集PingResultDto类转换成PingConnectReal类
	 * @param list
	 * @return
	 */
	public static List<PingConnectReal> pingResultToConnectRealList(List<PingResultDto> listDto) {
		List<PingConnectReal> listPojo = new ArrayList<PingConnectReal>();
		try {
			if(listDto == null || listDto.size() <= 0) {
				return listPojo;
			}
			for (PingResultDto pingResultDto : listDto) {
				listPojo.add(pingResultToConnectReal(pingResultDto));
			}
		}catch(Exception ex){
			logger.info(String.format("pingResultToConnectRealList error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return listPojo;
		}
		return listPojo;
	}
	/**
	 * 将ping结果集PingResultDto类转换成PingConnectReal类
	 * @return PingConnectRealDto
	 */
	public static PingConnectReal pingResultToConnectReal(PingResultDto pingResultDto){
		PingConnectReal pingConnectReal = new PingConnectReal();
		try {
			if(pingResultDto == null) {
				return pingConnectReal;
			}
        	String devCategoryCode = EnumDicPublic.DEV_CATEGORY.getKey();
        	String devCategoryName = EnumDicPublic.DEV_CATEGORY.getValue();
        	String devTypeCode = pingResultDto.getDevTypeCode();
        	double lossPacketRate = pingResultDto.getLossPacketRate();
        	int avgTime = pingResultDto.getAvgTime(); 
        	Boolean isPingConnect = true;
        	if(lossPacketRate == (double)1.0) {
        		isPingConnect = false;
        	}
        	pingConnectReal.setDevCategoryCode(devCategoryCode);
        	pingConnectReal.setDevCategoryName(devCategoryName);
        	pingConnectReal.setDevTypeCode(devTypeCode);
        	pingConnectReal.setDevTypeName(GetObjectHelper.getNameByTypeCode(devTypeCode));
        	pingConnectReal.setDevId(pingResultDto.getDevId());
        	pingConnectReal.setDevName(pingResultDto.getDevName());
        	pingConnectReal.setIsPingConnect(isPingConnect);
        	pingConnectReal.setPackageProb(lossPacketRate*100);
        	pingConnectReal.setResponseTime(avgTime);
		} catch(Exception ex) {
			logger.info(String.format("pingResultToConnectReal error: ",new Object[] { JSON.toJSONString(ex.getMessage()) }));
			return pingConnectReal;
		}
		return pingConnectReal;
	}
}
